<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>语音识别服务导航</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            max-width: 1200px;
            margin: 0 auto;
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .links-container {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
        }
        .link-card {
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            padding: 20px;
            transition: transform 0.2s, box-shadow 0.2s;
            position: relative;
        }
        .link-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.15);
        }
        .link-title {
            font-size: 18px;
            font-weight: bold;
            margin-bottom: 10px;
            color: #444;
        }
        .link-description {
            color: #666;
            font-size: 14px;
            margin-bottom: 15px;
        }
        .link-button {
            display: inline-block;
            padding: 8px 16px;
            background-color: #4CAF50;
            color: white;
            text-decoration: none;
            border-radius: 4px;
            font-size: 14px;
            transition: background-color 0.2s;
        }
        .link-button:hover {
            background-color: #45a049;
        }
        .add-card {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            cursor: pointer;
            background-color: #f9f9f9;
            border: 2px dashed #ccc;
        }
        .add-icon {
            font-size: 40px;
            color: #999;
            margin-bottom: 10px;
        }
        .add-text {
            color: #999;
        }
        .modal {
            display: none;
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            z-index: 1000;
            justify-content: center;
            align-items: center;
        }
        .modal-content {
            background-color: white;
            padding: 20px;
            border-radius: 8px;
            width: 400px;
            max-width: 90%;
        }
        .modal-title {
            margin-top: 0;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input, textarea {
            width: 100%;
            padding: 8px;
            border: 1px solid #ddd;
            border-radius: 4px;
            box-sizing: border-box;
        }
        .button-group {
            display: flex;
            justify-content: flex-end;
            gap: 10px;
            margin-top: 20px;
        }
        .btn {
            padding: 8px 16px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        .btn-cancel {
            background-color: #f1f1f1;
            color: #333;
        }
        .btn-submit {
            background-color: #4CAF50;
            color: white;
        }
        .delete-icon {
            position: absolute;
            top: 10px;
            right: 10px;
            cursor: pointer;
            color: #999;
            font-size: 16px;
        }
        .delete-icon:hover {
            color: #ff4444;
        }
        
        /* 编辑按钮样式 */
        .edit-icon {
            position: absolute;
            top: 10px;
            right: 40px;
            cursor: pointer;
            color: #999;
            font-size: 16px;
        }
        .edit-icon:hover {
            color: #4CAF50;
        }
        
        /* 链接提示样式 */
        .url-examples {
            font-size: 12px;
            color: #888;
            margin-top: 5px;
        }
        
        .url-example {
            display: block;
            margin: 2px 0;
            font-style: italic;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>语音识别服务导航</h1>
        
        <div class="links-container" id="linksContainer">
            <!-- 默认链接卡片 - API文档 -->
            <div class="link-card">
                <span class="delete-icon" onclick="deleteCard(this)">✕</span>
                <span class="edit-icon" onclick="editCard(this)">✎</span>
                <div class="link-title">API文档</div>
                <div class="link-description">查看API接口文档，了解如何使用语音识别服务。</div>
                <a href="/docs" target="_blank" class="link-button">打开文档</a>
            </div>
            
            <!-- 默认链接卡片 - WebSocket客户端 -->
            <div class="link-card">
                <span class="delete-icon" onclick="deleteCard(this)">✕</span>
                <span class="edit-icon" onclick="editCard(this)">✎</span>
                <div class="link-title">WebSocket客户端</div>
                <div class="link-description">使用WebSocket客户端测试实时语音识别功能。</div>
                <a href="/static/websocket_client.html" target="_blank" class="link-button">打开客户端</a>
            </div>
            
            <!-- 添加新链接的卡片 -->
            <div class="link-card add-card" onclick="openAddModal()">
                <div class="add-icon">+</div>
                <div class="add-text">添加新链接</div>
            </div>
        </div>
    </div>
    
    <!-- 添加/编辑链接的模态框 -->
    <div class="modal" id="addModal">
        <div class="modal-content">
            <h2 class="modal-title" id="modalTitle">添加新链接</h2>
            <form id="addLinkForm">
                <div class="form-group">
                    <label for="linkTitle">标题</label>
                    <input type="text" id="linkTitle" required placeholder="例如：模型管理">
                </div>
                <div class="form-group">
                    <label for="linkDescription">描述</label>
                    <textarea id="linkDescription" rows="3" required placeholder="简短描述这个链接的功能"></textarea>
                </div>
                <div class="form-group">
                    <label for="linkUrl">URL</label>
                    <input type="text" id="linkUrl" required placeholder="例如：/admin/models 或 static/websocket_client.html">
                    <div class="url-examples">
                        <span>常用URL格式示例：</span>
                        <span class="url-example">- 静态页面：static/页面名.html (例如：static/websocket_client.html)</span>
                        <span class="url-example">- 也可以写成：/static/页面名.html</span>
                        <span class="url-example">- API文档：/docs</span>
                        <span class="url-example">- API端点：/asr-api/接口名 (例如：/asr-api/recognize)</span>
                        <span class="url-example">- 外部链接：https://完整网址</span>
                    </div>
                </div>
                <input type="hidden" id="editIndex" value="-1">
                <div class="button-group">
                    <button type="button" class="btn btn-cancel" onclick="closeAddModal()">取消</button>
                    <button type="submit" class="btn btn-submit" id="submitBtn">添加</button>
                </div>
            </form>
        </div>
    </div>

    <script>
        // 打开添加链接模态框
        function openAddModal() {
            document.getElementById('modalTitle').innerText = '添加新链接';
            document.getElementById('submitBtn').innerText = '添加';
            document.getElementById('editIndex').value = '-1';
            document.getElementById('addLinkForm').reset();
            document.getElementById('addModal').style.display = 'flex';
        }
        
        // 关闭添加链接模态框
        function closeAddModal() {
            document.getElementById('addModal').style.display = 'none';
            document.getElementById('addLinkForm').reset();
        }
        
        // 删除链接卡片
        function deleteCard(element) {
            if (confirm('确定要删除这个链接吗？')) {
                element.parentElement.remove();
                saveLinks();
            }
        }
        
        // 编辑链接卡片
        function editCard(element) {
            const card = element.parentElement;
            const title = card.querySelector('.link-title').textContent;
            const description = card.querySelector('.link-description').textContent;
            const url = card.querySelector('.link-button').getAttribute('href');
            
            // 获取卡片在容器中的索引（用于后续更新）
            const cards = Array.from(document.querySelectorAll('.link-card:not(.add-card)'));
            const index = cards.indexOf(card);
            
            // 填充表单
            document.getElementById('linkTitle').value = title;
            document.getElementById('linkDescription').value = description;
            document.getElementById('linkUrl').value = url;
            document.getElementById('editIndex').value = index;
            
            // 更新模态框标题和按钮
            document.getElementById('modalTitle').innerText = '编辑链接';
            document.getElementById('submitBtn').innerText = '保存';
            
            // 显示模态框
            document.getElementById('addModal').style.display = 'flex';
        }
        
        // 添加/编辑链接表单提交处理
        document.getElementById('addLinkForm').addEventListener('submit', function(e) {
            e.preventDefault();
            
            const title = document.getElementById('linkTitle').value;
            const description = document.getElementById('linkDescription').value;
            let url = document.getElementById('linkUrl').value;
            const editIndex = parseInt(document.getElementById('editIndex').value);
            
            // 处理URL前缀问题，防止重复的/static
            if (url.startsWith('static/')) {
                url = '/' + url; // 添加前导斜杠
            } else if (!url.startsWith('/') && !url.startsWith('http')) {
                // 如果不是以/或http开头，自动添加/
                url = '/' + url;
            }
            
            if (editIndex >= 0) {
                // 编辑现有卡片
                const cards = document.querySelectorAll('.link-card:not(.add-card)');
                if (editIndex < cards.length) {
                    const card = cards[editIndex];
                    card.querySelector('.link-title').textContent = title;
                    card.querySelector('.link-description').textContent = description;
                    card.querySelector('.link-button').setAttribute('href', url);
                }
            } else {
                // 创建新的链接卡片
                const newCard = document.createElement('div');
                newCard.className = 'link-card';
                newCard.innerHTML = `
                    <span class="delete-icon" onclick="deleteCard(this)">✕</span>
                    <span class="edit-icon" onclick="editCard(this)">✎</span>
                    <div class="link-title">${title}</div>
                    <div class="link-description">${description}</div>
                    <a href="${url}" target="_blank" class="link-button">打开链接</a>
                `;
                
                // 获取链接容器
                const container = document.getElementById('linksContainer');
                
                // 将新卡片插入到"添加新链接"卡片之前
                container.insertBefore(newCard, document.querySelector('.add-card'));
            }
            
            // 关闭模态框
            closeAddModal();
            
            // 保存到本地存储
            saveLinks();
        });
        
        // 保存链接到本地存储
        function saveLinks() {
            const links = [];
            const linkCards = document.querySelectorAll('.link-card:not(.add-card)');
            
            linkCards.forEach(card => {
                links.push({
                    title: card.querySelector('.link-title').textContent,
                    description: card.querySelector('.link-description').textContent,
                    url: card.querySelector('.link-button').getAttribute('href')
                });
            });
            
            localStorage.setItem('navLinks', JSON.stringify(links));
        }
        
        // 从本地存储加载链接
        function loadLinks() {
            const savedLinks = localStorage.getItem('navLinks');
            
            if (savedLinks) {
                const links = JSON.parse(savedLinks);
                const container = document.getElementById('linksContainer');
                
                // 清除现有的链接卡片
                const existingCards = document.querySelectorAll('.link-card:not(.add-card)');
                existingCards.forEach(card => card.remove());
                
                // 添加保存的链接
                links.forEach(link => {
                    const newCard = document.createElement('div');
                    newCard.className = 'link-card';
                    newCard.innerHTML = `
                        <span class="delete-icon" onclick="deleteCard(this)">✕</span>
                        <span class="edit-icon" onclick="editCard(this)">✎</span>
                        <div class="link-title">${link.title}</div>
                        <div class="link-description">${link.description}</div>
                        <a href="${link.url}" target="_blank" class="link-button">打开链接</a>
                    `;
                    
                    // 将新卡片插入到"添加新链接"卡片之前
                    container.insertBefore(newCard, document.querySelector('.add-card'));
                });
            }
        }
        
        // 页面加载时加载保存的链接
        window.addEventListener('load', loadLinks);
    </script>
</body>
</html>
